

package org.apache.ocean.server;

import org.apache.lucene.search.highlight.*;
import org.apache.lucene.analysis.Token;


/**
 * A simple modification of SimpleFragmenter which additionally creates new
 * fragments when an unusually-large position increment is encountered
 * (this behaves much better in the presence of multi-valued fields).
 */
public class GapFragmenter extends SimpleFragmenter {
  public static final int INCREMENT_THRESHOLD = 50;
  protected int fragOffsetAccum = 0;
  
  public GapFragmenter(int i) {
    super(i);
  }
  
  /* (non-Javadoc)
   * @see org.apache.lucene.search.highlight.TextFragmenter#start(java.lang.String)
   */
  public void start(String originalText) {
    fragOffsetAccum = 0;
  }
  
  /* (non-Javadoc)
   * @see org.apache.lucene.search.highlight.TextFragmenter#isNewFragment(org.apache.lucene.analysis.Token)
   */
  public boolean isNewFragment(Token token) {
    boolean isNewFrag =
    token.endOffset() >= fragOffsetAccum + getFragmentSize() ||
    token.getPositionIncrement() > INCREMENT_THRESHOLD;
    if(isNewFrag) {
      fragOffsetAccum += token.endOffset() - fragOffsetAccum;
    }
    return isNewFrag;
  }
}